import React, { useRef, useState } from 'react';
import { useFrame, useThree } from '@react-three/fiber';
import { Text, Box, Sphere, Cylinder, Cone, Plane, RoundedBox } from '@react-three/drei';
import type { DatingProfile } from '../types';
import * as THREE from 'three';

interface UltraMaleCharacterProps {
  profile: DatingProfile;
  position: [number, number, number];
  rotation?: [number, number, number];
  onClick: () => void;
  scale?: number;
  isSeated?: boolean;
}

export const UltraMaleCharacter3D: React.FC<UltraMaleCharacterProps> = ({
  profile,
  position,
  rotation = [0, 0, 0],
  onClick,
  scale = 1,
  isSeated = false
}) => {
  const characterRef = useRef<THREE.Group>(null);
  const [hovered, setHovered] = useState(false);
  const [clicked, setClicked] = useState(false);
  const { camera } = useThree();

  // 稳定的动画系统 - 防止后倾
  useFrame((state) => {
    if (characterRef.current) {
      // 最小化呼吸动画，防止不稳定
      const breathScale = 1 + Math.sin(state.clock.elapsedTime * 0.8) * 0.003;
      characterRef.current.scale.set(scale * breathScale, scale * breathScale, scale * breathScale);
      
      // 移除所有旋转动画，保持稳定
      if (isSeated) {
        // 坐着时保持完全静止
        characterRef.current.rotation.set(rotation[0], rotation[1], rotation[2]);
      }
      
      // 悬停时只调整头部朝向，不影响整体稳定性
      if (hovered && camera) {
        // 只在垂直方向上有微小调整
        const lookDirection = new THREE.Vector3().subVectors(camera.position, characterRef.current.position);
        lookDirection.y = Math.max(0, Math.min(lookDirection.y, 0.5)); // 限制头部转动范围
      }
    }
  });

  const handleClick = () => {
    setClicked(true);
    setTimeout(() => setClicked(false), 300);
    onClick();
  };

  const handlePointerOver = () => {
    setHovered(true);
    document.body.style.cursor = 'pointer';
  };

  const handlePointerOut = () => {
    setHovered(false);
    document.body.style.cursor = 'default';
  };

  // 专业级材质系统
  const materials = {
    skin: { color: '#F4C2A1', roughness: 0.85, metalness: 0.02 },
    hair: { color: '#2C1810', roughness: 0.95, metalness: 0.0 },
    shirt: { color: '#1A365D', roughness: 0.7, metalness: 0.1 },
    suit: { color: '#1A202C', roughness: 0.6, metalness: 0.05 },
    tie: { color: '#C53030', roughness: 0.5, metalness: 0.1 },
    pants: { color: '#2D3748', roughness: 0.8, metalness: 0.0 },
    shoes: { color: '#000000', roughness: 0.3, metalness: 0.8 },
    metal: { color: '#718096', roughness: 0.1, metalness: 0.9 },
    gold: { color: '#D69E2E', roughness: 0.1, metalness: 0.95 }
  };

  // 坐姿偏移 - 精确调整
  const sittingAdjust = isSeated ? { torso: -0.15, legs: -0.25, feet: -0.4 } : { torso: 0, legs: 0, feet: 0 };

  return (
    <group 
      ref={characterRef}
      position={position}
      rotation={rotation}
      onClick={handleClick}
      onPointerOver={handlePointerOver}
      onPointerOut={handlePointerOut}
    >
      {/* ========== 精确身体建模系统 ========== */}
      
      {/* 核心躯干 - 解剖学精确 */}
      <group position={[0, 0.75 + sittingAdjust.torso, 0]}>
        {/* 胸部 - 男性体型 */}
        <Box position={[0, 0.15, 0]} args={[0.48, 0.32, 0.22]} castShadow receiveShadow>
          <meshStandardMaterial {...materials.shirt} />
        </Box>
        
        {/* 腹部 */}
        <Box position={[0, -0.05, 0]} args={[0.44, 0.25, 0.2]} castShadow receiveShadow>
          <meshStandardMaterial {...materials.shirt} />
        </Box>
        
        {/* 腰部 */}
        <Box position={[0, -0.22, 0]} args={[0.4, 0.18, 0.18]} castShadow receiveShadow>
          <meshStandardMaterial {...materials.pants} />
        </Box>

        {/* 衬衫领口细节 */}
        <Box position={[0, 0.28, 0.12]} args={[0.3, 0.12, 0.02]} castShadow>
          <meshStandardMaterial color="#FFFFFF" roughness={0.4} />
        </Box>
        
        {/* 西装外套 */}
        <Box position={[0, 0.1, 0.01]} args={[0.52, 0.4, 0.25]} castShadow>
          <meshStandardMaterial {...materials.suit} />
        </Box>
        
        {/* 西装翻领 */}
        <Box position={[-0.15, 0.22, 0.13]} args={[0.15, 0.2, 0.02]} rotation={[0, 0, 0.3]} castShadow>
          <meshStandardMaterial {...materials.suit} />
        </Box>
        <Box position={[0.15, 0.22, 0.13]} args={[0.15, 0.2, 0.02]} rotation={[0, 0, -0.3]} castShadow>
          <meshStandardMaterial {...materials.suit} />
        </Box>

        {/* 领带系统 - 精细建模 */}
        <group position={[0, 0.05, 0.14]}>
          <Box args={[0.06, 0.35, 0.012]} castShadow>
            <meshStandardMaterial {...materials.tie} />
          </Box>
          {/* 领带结 */}
          <Box position={[0, 0.16, 0.008]} args={[0.1, 0.05, 0.02]} castShadow>
            <meshStandardMaterial {...materials.tie} />
          </Box>
          {/* 领带夹 */}
          <Box position={[0, 0.08, 0.01]} args={[0.08, 0.01, 0.005]} castShadow>
            <meshStandardMaterial {...materials.gold} />
          </Box>
        </group>

        {/* 西装纽扣 */}
        {[0.05, -0.05, -0.15].map((y, i) => (
          <Cylinder
            key={i}
            position={[0, y, 0.13]}
            args={[0.012, 0.012, 0.008]}
            rotation={[Math.PI/2, 0, 0]}
            castShadow
          >
            <meshStandardMaterial {...materials.gold} />
          </Cylinder>
        ))}
      </group>

      {/* ========== 精确手臂建模 ========== */}
      
      {/* 左臂完整系统 */}
      <group>
        {/* 肩膀连接 */}
        <Sphere position={[-0.3, 0.85 + sittingAdjust.torso, 0]} args={[0.08]} castShadow>
          <meshStandardMaterial {...materials.suit} />
        </Sphere>
        
        {/* 上臂 - 肌肉感 */}
        <Cylinder
          position={[-0.4, 0.65 + sittingAdjust.torso, 0]}
          args={[0.07, 0.08, 0.32]}
          rotation={[0, 0, isSeated ? 0.4 : 0.1]}
          castShadow
        >
          <meshStandardMaterial {...materials.suit} />
        </Cylinder>
        
        {/* 肘部关节 */}
        <Sphere 
          position={[-0.48, 0.48 + sittingAdjust.torso, isSeated ? 0.08 : 0]} 
          args={[0.06]} 
          castShadow
        >
          <meshStandardMaterial {...materials.suit} />
        </Sphere>
        
        {/* 前臂 */}
        <Cylinder
          position={[-0.55, 0.32 + sittingAdjust.torso, isSeated ? 0.12 : 0]}
          args={[0.055, 0.065, 0.28]}
          rotation={[0, 0, isSeated ? -0.3 : 0.05]}
          castShadow
        >
          <meshStandardMaterial {...materials.shirt} />
        </Cylinder>
        
        {/* 手腕 */}
        <Sphere 
          position={[-0.62, 0.18 + sittingAdjust.torso, isSeated ? 0.18 : 0]} 
          args={[0.05]} 
          castShadow
        >
          <meshStandardMaterial {...materials.skin} />
        </Sphere>
        
        {/* 左手详细建模 */}
        <group position={[-0.66, 0.15 + sittingAdjust.torso, isSeated ? 0.22 : 0]}>
          {/* 手掌 */}
          <Box args={[0.08, 0.06, 0.12]} castShadow>
            <meshStandardMaterial {...materials.skin} />
          </Box>
          
          {/* 五指建模 */}
          {Array.from({ length: 5 }, (_, i) => (
            <Cylinder
              key={i}
              position={[
                -0.03 + (i * 0.015),
                0,
                0.08 + (i === 2 ? 0.02 : i === 1 || i === 3 ? 0.01 : 0)
              ]}
              args={[0.008, 0.008, i === 0 ? 0.025 : 0.035]}
              castShadow
            >
              <meshStandardMaterial {...materials.skin} />
            </Cylinder>
          ))}
          
          {/* 指甲细节 */}
          {Array.from({ length: 5 }, (_, i) => (
            <Sphere
              key={i}
              position={[
                -0.03 + (i * 0.015),
                0,
                0.1 + (i === 2 ? 0.025 : i === 1 || i === 3 ? 0.015 : 0.005)
              ]}
              args={[0.005]}
              castShadow
            >
              <meshStandardMaterial color="#FFE4E1" roughness={0.2} metalness={0.1} />
            </Sphere>
          ))}
        </group>

        {/* 高级手表系统 */}
        <group position={[-0.62, 0.22 + sittingAdjust.torso, isSeated ? 0.18 : 0]}>
          {/* 表带 */}
          <Cylinder args={[0.055, 0.055, 0.03]} rotation={[Math.PI/2, 0, 0]} castShadow>
            <meshStandardMaterial color="#2F4F4F" roughness={0.6 } />
          </Cylinder>
          
          {/* 表盘 */}
          <Cylinder args={[0.04, 0.04, 0.015]} rotation={[Math.PI/2, 0, 0]} castShadow>
            <meshStandardMaterial {...materials.metal} />
          </Cylinder>
          
          {/* 表面 */}
          <Cylinder args={[0.035, 0.035, 0.005]} rotation={[Math.PI/2, 0, 0]} position={[0, 0, 0.01]}>
            <meshStandardMaterial color="#000000" roughness={0.1} metalness={0.8} />
          </Cylinder>
          
          {/* 时针分针 */}
          <Box position={[0, 0, 0.015]} args={[0.002, 0.02, 0.002]} castShadow>
            <meshStandardMaterial {...materials.gold} />
          </Box>
          <Box position={[0, 0, 0.015]} args={[0.025, 0.001, 0.002]} rotation={[0, 0, Math.PI/6]} castShadow>
            <meshStandardMaterial {...materials.gold} />
          </Box>
        </group>
      </group>

      {/* 右臂系统 - 对称但有细微差别 */}
      <group>
        <Sphere position={[0.3, 0.85 + sittingAdjust.torso, 0]} args={[0.08]} castShadow>
          <meshStandardMaterial {...materials.suit} />
        </Sphere>
        
        <Cylinder
          position={[0.4, 0.65 + sittingAdjust.torso, 0]}
          args={[0.07, 0.08, 0.32]}
          rotation={[0, 0, isSeated ? -0.4 : -0.1]}
          castShadow
        >
          <meshStandardMaterial {...materials.suit} />
        </Cylinder>
        
        <Sphere 
          position={[0.48, 0.48 + sittingAdjust.torso, isSeated ? 0.08 : 0]} 
          args={[0.06]} 
          castShadow
        >
          <meshStandardMaterial {...materials.suit} />
        </Sphere>
        
        <Cylinder
          position={[0.55, 0.32 + sittingAdjust.torso, isSeated ? 0.12 : 0]}
          args={[0.055, 0.065, 0.28]}
          rotation={[0, 0, isSeated ? 0.3 : -0.05]}
          castShadow
        >
          <meshStandardMaterial {...materials.shirt} />
        </Cylinder>
        
        <Sphere 
          position={[0.62, 0.18 + sittingAdjust.torso, isSeated ? 0.18 : 0]} 
          args={[0.05]} 
          castShadow
        >
          <meshStandardMaterial {...materials.skin} />
        </Sphere>
        
        {/* 右手 */}
        <group position={[0.66, 0.15 + sittingAdjust.torso, isSeated ? 0.22 : 0]}>
          <Box args={[0.08, 0.06, 0.12]} castShadow>
            <meshStandardMaterial {...materials.skin} />
          </Box>
          
          {Array.from({ length: 5 }, (_, i) => (
            <Cylinder
              key={i}
              position={[
                -0.03 + (i * 0.015),
                0,
                0.08 + (i === 2 ? 0.02 : i === 1 || i === 3 ? 0.01 : 0)
              ]}
              args={[0.008, 0.008, i === 0 ? 0.025 : 0.035]}
              castShadow
            >
              <meshStandardMaterial {...materials.skin} />
            </Cylinder>
          ))}
        </group>
      </group>

      {/* ========== 精确腿部建模 ========== */}
      
      {/* 臀部连接 */}
      <Box position={[0, 0.45 + sittingAdjust.legs, 0]} args={[0.36, 0.15, 0.2]} castShadow>
        <meshStandardMaterial {...materials.pants} />
      </Box>

      {/* 大腿系统 */}
      <group>
        <Cylinder
          position={[-0.1, 0.25 + sittingAdjust.legs, isSeated ? 0.1 : 0]}
          args={[0.08, 0.09, isSeated ? 0.3 : 0.35]}
          rotation={isSeated ? [0.5, 0, 0] : [0, 0, 0]}
          castShadow
        >
          <meshStandardMaterial {...materials.pants} />
        </Cylinder>
        
        <Cylinder
          position={[0.1, 0.25 + sittingAdjust.legs, isSeated ? 0.1 : 0]}
          args={[0.08, 0.09, isSeated ? 0.3 : 0.35]}
          rotation={isSeated ? [0.5, 0, 0] : [0, 0, 0]}
          castShadow
        >
          <meshStandardMaterial {...materials.pants} />
        </Cylinder>
      </group>

      {/* 膝盖 - 只有站立时 */}
      {!isSeated && (
        <group>
          <Sphere position={[-0.1, 0.12, 0]} args={[0.06]} castShadow>
            <meshStandardMaterial {...materials.pants} />
          </Sphere>
          <Sphere position={[0.1, 0.12, 0]} args={[0.06]} castShadow>
            <meshStandardMaterial {...materials.pants} />
          </Sphere>
        </group>
      )}

      {/* 小腿 - 只有站立时 */}
      {!isSeated && (
        <group>
          <Cylinder position={[-0.1, 0.06, 0]} args={[0.065, 0.075, 0.12]} castShadow>
            <meshStandardMaterial {...materials.pants} />
          </Cylinder>
          <Cylinder position={[0.1, 0.06, 0]} args={[0.065, 0.075, 0.12]} castShadow>
            <meshStandardMaterial {...materials.pants} />
          </Cylinder>
        </group>
      )}

      {/* 高档皮鞋 - 只有站立时 */}
      {!isSeated && (
        <group position={[0, sittingAdjust.feet, 0]}>
          {/* 左鞋 */}
          <group position={[-0.1, 0.04, 0.05]}>
            <RoundedBox args={[0.14, 0.08, 0.28]} radius={0.02} castShadow>
              <meshStandardMaterial {...materials.shoes} />
            </RoundedBox>
            {/* 鞋带 */}
            {Array.from({ length: 4 }, (_, i) => (
              <Box
                key={i}
                position={[0, 0.05, -0.05 + i * 0.03]}
                args={[0.08, 0.002, 0.002]}
                castShadow
              >
                <meshStandardMaterial color="#8B4513" />
              </Box>
            ))}
            {/* 鞋底纹理 */}
            <Box position={[0, -0.05, 0]} args={[0.15, 0.02, 0.3]} castShadow>
              <meshStandardMaterial color="#2F2F2F" roughness={0.9} />
            </Box>
          </group>
          
          {/* 右鞋 */}
          <group position={[0.1, 0.04, 0.05]}>
            <RoundedBox args={[0.14, 0.08, 0.28]} radius={0.02} castShadow>
              <meshStandardMaterial {...materials.shoes} />
            </RoundedBox>
            {Array.from({ length: 4 }, (_, i) => (
              <Box
                key={i}
                position={[0, 0.05, -0.05 + i * 0.03]}
                args={[0.08, 0.002, 0.002]}
                castShadow
              >
                <meshStandardMaterial color="#8B4513" />
              </Box>
            ))}
            <Box position={[0, -0.05, 0]} args={[0.15, 0.02, 0.3]} castShadow>
              <meshStandardMaterial color="#2F2F2F" roughness={0.9} />
            </Box>
          </group>
        </group>
      )}

      {/* ========== 超精细头部建模 ========== */}
      
      {/* 颈部 */}
      <Cylinder
        position={[0, 1.0 + sittingAdjust.torso, 0]}
        args={[0.06, 0.06, 0.12]}
        castShadow
      >
        <meshStandardMaterial {...materials.skin} />
      </Cylinder>

      {/* 头部基础 - 真实头型 */}
      <Sphere
        position={[0, 1.15 + sittingAdjust.torso, 0]}
        args={[0.16, 24, 24]}
        castShadow
      >
        <meshStandardMaterial {...materials.skin} />
      </Sphere>

      {/* 男性发型系统 */}
      <group position={[0, 1.15 + sittingAdjust.torso, 0]}>
        {/* 主发型 */}
        <Sphere position={[0, 0.08, -0.05]} args={[0.17, 16, 16]} castShadow>
          <meshStandardMaterial {...materials.hair} />
        </Sphere>
        
        {/* 侧面修剪 */}
        <Sphere position={[-0.13, 0.02, -0.08]} args={[0.08, 12, 12]} castShadow>
          <meshStandardMaterial {...materials.hair} />
        </Sphere>
        <Sphere position={[0.13, 0.02, -0.08]} args={[0.08, 12, 12]} castShadow>
          <meshStandardMaterial {...materials.hair} />
        </Sphere>
        
        {/* 后脑勺 */}
        <Sphere position={[0, 0, -0.15]} args={[0.14, 16, 16]} castShadow>
          <meshStandardMaterial {...materials.hair} />
        </Sphere>
        
        {/* 发际线细节 */}
        <Box position={[0, 0.12, 0.1]} args={[0.22, 0.04, 0.04]} castShadow>
          <meshStandardMaterial {...materials.hair} />
        </Box>
      </group>

      {/* ========== 超精细面部特征 ========== */}
      
      <group position={[0, 1.15 + sittingAdjust.torso, 0]}>
        {/* 眼窝结构 */}
        <Box position={[-0.045, 0.02, 0.135]} args={[0.04, 0.025, 0.02]} castShadow>
          <meshStandardMaterial color="#F5DEB3" roughness={0.8} />
        </Box>
        <Box position={[0.045, 0.02, 0.135]} args={[0.04, 0.025, 0.02]} castShadow>
          <meshStandardMaterial color="#F5DEB3" roughness={0.8} />
        </Box>
        
        {/* 眼白 */}
        <Sphere position={[-0.045, 0.02, 0.145]} args={[0.018, 12, 12]}>
          <meshStandardMaterial color="#FFFFFF" roughness={0.3} />
        </Sphere>
        <Sphere position={[0.045, 0.02, 0.145]} args={[0.018, 12, 12]}>
          <meshStandardMaterial color="#FFFFFF" roughness={0.3} />
        </Sphere>
        
        {/* 虹膜 */}
        <Sphere position={[-0.045, 0.02, 0.155]} args={[0.012, 10, 10]}>
          <meshStandardMaterial color="#4A5568" roughness={0.2} />
        </Sphere>
        <Sphere position={[0.045, 0.02, 0.155]} args={[0.012, 10, 10]}>
          <meshStandardMaterial color="#4A5568" roughness={0.2} />
        </Sphere>
        
        {/* 瞳孔 */}
        <Sphere position={[-0.045, 0.02, 0.16]} args={[0.006, 8, 8]}>
          <meshStandardMaterial color="#000000" />
        </Sphere>
        <Sphere position={[0.045, 0.02, 0.16]} args={[0.006, 8, 8]}>
          <meshStandardMaterial color="#000000" />
        </Sphere>
        
        {/* 眼部高光 */}
        <Sphere position={[-0.042, 0.025, 0.162]} args={[0.004, 6, 6]}>
          <meshBasicMaterial color="#FFFFFF" />
        </Sphere>
        <Sphere position={[0.048, 0.025, 0.162]} args={[0.004, 6, 6]}>
          <meshBasicMaterial color="#FFFFFF" />
        </Sphere>
        
        {/* 上眼皮 */}
        <Box position={[-0.045, 0.035, 0.15]} args={[0.025, 0.008, 0.015]} castShadow>
          <meshStandardMaterial {...materials.skin} />
        </Box>
        <Box position={[0.045, 0.035, 0.15]} args={[0.025, 0.008, 0.015]} castShadow>
          <meshStandardMaterial {...materials.skin} />
        </Box>

        {/* 眉毛 - 立体 */}
        <Box position={[-0.045, 0.06, 0.14]} args={[0.03, 0.008, 0.008]} castShadow>
          <meshStandardMaterial {...materials.hair} />
        </Box>
        <Box position={[0.045, 0.06, 0.14]} args={[0.03, 0.008, 0.008]} castShadow>
          <meshStandardMaterial {...materials.hair} />
        </Box>
        
        {/* 鼻梁 */}
        <Box position={[0, 0.01, 0.15]} args={[0.015, 0.04, 0.015]} castShadow>
          <meshStandardMaterial {...materials.skin} />
        </Box>
        
        {/* 鼻头 */}
        <Sphere position={[0, -0.015, 0.158]} args={[0.012, 8, 8]} castShadow>
          <meshStandardMaterial {...materials.skin} />
        </Sphere>
        
        {/* 鼻孔 */}
        <Sphere position={[-0.006, -0.02, 0.16]} args={[0.003, 6, 6]}>
          <meshStandardMaterial color="#8B4513" />
        </Sphere>
        <Sphere position={[0.006, -0.02, 0.16]} args={[0.003, 6, 6]}>
          <meshStandardMaterial color="#8B4513" />
        </Sphere>

        {/* 嘴部建模 */}
        <Box position={[0, -0.08, 0.145]} args={[0.04, 0.01, 0.008]} castShadow>
          <meshStandardMaterial color="#CD853F" roughness={0.4} />
        </Box>
        
        {/* 牙齿暗示 */}
        <Box position={[0, -0.08, 0.15]} args={[0.035, 0.005, 0.003]}>
          <meshStandardMaterial color="#FFFACD" roughness={0.3} />
        </Box>

        {/* 下巴轮廓 */}
        <Box position={[0, -0.12, 0.12]} args={[0.18, 0.08, 0.12]} castShadow>
          <meshStandardMaterial {...materials.skin} />
        </Box>
        
        {/* 颧骨 */}
        <Sphere position={[-0.08, -0.01, 0.12]} args={[0.025, 8, 8]} castShadow>
          <meshStandardMaterial {...materials.skin} />
        </Sphere>
        <Sphere position={[0.08, -0.01, 0.12]} args={[0.025, 8, 8]} castShadow>
          <meshStandardMaterial {...materials.skin} />
        </Sphere>
      </group>

      {/* ========== 交互效果系统 ========== */}
      
      {/* 悬停信息显示 */}
      {hovered && (
        <group position={[0, 1.7 + sittingAdjust.torso, 0]}>
          <Text
            fontSize={0.12}
            color="#2B6CB0"
            anchorX="center"
            anchorY="middle"
            castShadow
          >
            {profile.name}
          </Text>
          <Text
            position={[0, -0.16, 0]}
            fontSize={0.08}
            color="#4A5568"
            anchorX="center"
            anchorY="middle"
          >
            {profile.age}岁 • {profile.occupation}
          </Text>
        </group>
      )}

      {/* 稳定的悬停光环 */}
      {hovered && (
        <Cylinder 
          position={[0, 0.05, 0]} 
          args={[1.2, 1.2, 0.05]}
          rotation={[Math.PI/2, 0, 0]}
        >
          <meshLambertMaterial
            color="#3182CE"
            transparent
            opacity={0.1}
            side={THREE.DoubleSide}
          />
        </Cylinder>
      )}

      {/* 点击特效 */}
      {clicked && (
        <>
          {Array.from({ length: 6 }, (_, i) => (
            <Text
              key={i}
              position={[
                (Math.random() - 0.5) * 2,
                0.8 + Math.random() * 0.8 + sittingAdjust.torso,
                (Math.random() - 0.5) * 2
              ]}
              fontSize={0.15}
              color="#2B6CB0"
              anchorX="center"
              anchorY="middle"
            >
              💙
            </Text>
          ))}
        </>
      )}
    </group>
  );
};